package org.autumn.util;


import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * Cryptography util
 *
 * @author mikael
 */
public class DigestsUtil {

    /** encryption algorithm */
    public static final String SHA1 = "SHA-1";
    /** algorithm encryption numbers of time */
    public static final Integer LOOP_ENCRYPTION = 1;
    /** random generate salt */
    public static SecureRandomNumberGenerator random = new SecureRandomNumberGenerator();

    /**
     * do SHA1 encryption
     * @param pwd   password
     * @param salt  salt
     * @return String
     */
    public static String doSHA1(String pwd,String salt){
        return new SimpleHash(SHA1,pwd,salt,LOOP_ENCRYPTION).toString();
    }

    /**
     * get random salt
     * @return salt
     */
    public static String genSalt(){
       return random.nextBytes().toString();
    }

    /**
     * ncryption
     * @param pwd
     * @return
     */
    public static Digests encryption(String pwd){
        String salt = genSalt();
        return new Digests(doSHA1(pwd,salt),salt);
    }

    @Data
    @AllArgsConstructor
    public static class Digests{
        String password;
        String salt;
    }

    public static void main(String[] args) {
        System.out.println(DigestsUtil.encryption("123"));

    }
}
